package thermologic.file;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFDataFormat;
import org.apache.poi.hssf.usermodel.HSSFRichTextString;
import org.apache.poi.hssf.usermodel.HSSFRow;

import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import thermologic.core.Channel;
import thermologic.core.Datalogger;
import thermologic.core.Device;
import thermologic.core.Sample;

/**
 * Export the application data including
 * tracking and configuration data, and samples.
 * 
 * Uses the HSSF components from Apache POI 
 * 
 * @author mrivadeneira
 */
public class ExcelFileHandler implements FileHandler
{
    public void save(File file, Object objToExport) throws IOException {

        Device devices[] = (Device[])objToExport;
        
        HSSFWorkbook wb = new HSSFWorkbook();                
        
        for(Device d: devices)
        {
            Datalogger datalogger = (Datalogger)d;
            
            for(Channel channel: datalogger.getChannels())
            {
                HSSFSheet newSheet = wb.createSheet(
                            String.format(
                                "%s_%s", datalogger.getId(), channel.getLabel()
                            )
                        );
                
                int rowNum = 0;
                String format = java.util.ResourceBundle.getBundle("properties/language").
                        getString("thermologic.format.ExcelDateAndTime");
                
                HSSFRow row = newSheet.createRow(rowNum++); 
                
                HSSFCell cell = row.createCell(0);
                cell.setCellValue(new HSSFRichTextString(java.util.ResourceBundle.getBundle("properties/language").getString("thermologic.labels.dateTime")));
                
                cell = row.createCell(1);
                cell.setCellValue(new HSSFRichTextString(channel.toString()));
                
                
                for(Sample sample: channel.getSamples())
                {
                    row = newSheet.createRow(rowNum++);
                    
                   cell = row.createCell(0);
                    HSSFCellStyle cellStyle = wb.createCellStyle();
                    cellStyle.setDataFormat(HSSFDataFormat.getBuiltinFormat(format));
                    cellStyle.setWrapText(false);                    
                    cell.setCellValue(sample.getTimeStamp());
                    cell.setCellStyle(cellStyle);
                    
                    
                    cell = row.createCell(1);
                    cell.setCellValue(sample.getValue());
                }
                
                newSheet.autoSizeColumn((short)0);
                newSheet.autoSizeColumn((short)1);
            }
        }
        
        FileOutputStream fileOut = new FileOutputStream(file);
        wb.write(fileOut);
        fileOut.close();
    }
    

    public Object load(File file) throws IOException {
        throw new UnsupportedOperationException("Not supported yet.");
    }
    
}
